import { Toast } from "vant";
// vite中简写动态图片路径
export const getAssetsFile = (url: string,name='' as string) => {
    const fileName = name?name:import.meta.env.VITE_THEME
    return new URL(`../assets/${fileName}/${url}`, import.meta.url).href
}
// 用户名称6字省略
export const nameFilter = (val:any ='',num:number = 6,sign:number=3) => {
    let arr = val.split('...')
    if(arr[arr.length - 1] == '') return val
    if(val.length > num) {
        let signtext = ''
        for(let i=1;i<=sign;i++) {
            signtext+='.'
        }
        val = val.slice(0,num) + signtext
    }
    return val
};
export const ToUserAndRoom = (item:any = {})=>{
    const { user_id=0, room_id=0, room_open=0 } = item
    if(user_id) {
        return (kk as any).goCommon({ code: "100001", user_id });
    }
    if(room_id && room_open == 1) {
        let data = {
            code: 100009,
            action: `kkaudio://room/info?id=${room_id}&type=0`,
        };
        return (kk as any).goCommon(data);
    }
}
export const getNumCompany = (e: any)=> {
    if(!Number(e)) return e
    e = e * 1
    if (e > 99999999) {
      return Math.floor(e/100000000) / 100 + '亿'
    }
    if (e > 9999) {
      e = e / 100;
      return Math.floor(e) /100 + '万'
    }
    return e
  }
// 获取当前时间
export const getPresentTime = ()=> {
    const date = new Date('')
    let year = date.getFullYear();
    let month:any = date.getMonth() + 1;
    let day:any = date.getDate()
    month = month < 10 ? '0' + month : month
    day = day < 10 ? '0' + day : day
    let hour:any = date.getHours() // 时
    let branch:any = date.getMinutes(); // 分
    let second:any = date.getSeconds(); // 秒
    hour = hour < 10 ? '0' + hour : hour
    branch = branch < 10 ? '0' + branch : branch
    second = second < 10 ? '0' + second : second
    return {
        dateText:`${year}-${month}-${day}`,
        timeText:`${year}-${month}-${day} ${hour}:${branch}:${second}`,
        year,
        month,
        day,
        hour,
        branch,
        second
    }
}

// 获取当前时间戳
export const getPresentTimeNum = (time:any)=> {
    const date = new Date(time)
    let year = date.getFullYear();
    let month:any = date.getMonth() + 1;
    let day:any = date.getDate()
    month = month < 10 ? '0' + month : month
    day = day < 10 ? '0' + day : day
    return new Date(`${year}-${month}-${day}`).getTime()
}

// 获取当然时间戳的年月日 type = dhm + 时分秒
export const getTimestampTime = (time:any = '',type:String = '')=> {
    const date:any = new Date(time)
    let year = date.getFullYear(); // 年
    let month:any = date.getMonth() + 1; // 月
    let day:any = date.getDate(); // 日
    let hour:any = date.getHours() // 时
    let branch:any = date.getMinutes(); // 分
    let second:any = date.getSeconds(); // 秒
    month = month < 10 ? '0' + month : month
    day = day < 10 ? '0' + day : day
    hour = hour < 10 ? '0' + hour : hour
    branch = branch < 10 ? '0' + branch : branch
    second = second < 10 ? '0' + second : second
    let text = `${year}-${month}-${day} `
    if(type == 'dhm') text = `${year}-${month}-${day} ${hour}:${branch}:${second}`
    return text
}

// 获取当前时间段段结束时间
export const getanHourLater = (time:any = '')=> {
    const date:any = new Date(time)
    let year = date.getFullYear(); // 年
    let month:any = date.getMonth() + 1; // 月
    let day:any = date.getDate(); // 日
    let hour:any = date.getHours() // 时
    let branch:any = date.getMinutes(); // 分
    let second:any = date.getSeconds(); // 秒
    month = month < 10 ? '0' + month : month
    day = day < 10 ? '0' + day : day
    hour = hour < 10 ? '0' + hour : hour
    let text = `${year}/${month}/${day} ${hour}:59:59`
    return text
}

// 获取两个时间之差
export const getTimehour = (startTime:any='',endTime:any='') => {
    const currentNum:any = new Date(startTime);
    const endNum:any = new Date(endTime);
    const time = ( endNum - currentNum ) / 1000
    if(time == 0) return 'endtime'
    let day =  Math.floor(time / 60 / 60 / 24); // 天
    let hour:any = Math.floor(time / 60 / 60 % 24); // 时
    let branch:any = Math.floor(time / 60 % 60); // 分
    let second:any = Math.floor(time % 60); // 秒
    hour = hour < 10 ? '0' + hour : hour
    branch = branch < 10 ? '0' + branch : branch
    second = second < 10 ? '0' + second : second
    return `${day}天${hour}小时${branch}分${second}`
}
// 获取两个时间之差 返回obj格式
export const getTimehourObj = (startTime:any='',endTime:any='') => {
    const time = ( endTime - startTime )
    if(time <= 0) return 'endtime'
    let day =  Math.floor(time / 60 / 60 / 24); // 天
    let hour:any = Math.floor(time / 60 / 60 % 24); // 时
    let branch:any = Math.floor(time / 60 % 60); // 分
    let second:any = Math.floor(time % 60); // 秒
    hour = hour < 10 ? '0' + hour : hour
    branch = branch < 10 ? '0' + branch : branch
    second = second < 10 ? '0' + second : second
    return {day,hour,branch,second}
}
// vant提示框
export const xmtoast = (message:any = '') => {
    Toast({
      message,
      className:'xmToast',
    })
}
// vant加载框
export const xmloading = (message = '加载中...') => {
    Toast.loading({
        message,
        forbidClick: true,
        loadingType:'spinner',
        duration:0,
        className:'xmloading',
    })
}
export const iosOrAndroid = () => {
    let ios = !!navigator.userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
    let Android = navigator.userAgent.indexOf('Android') > -1 || navigator.userAgent.indexOf('Linux') > -1;
    if (ios) {
        return 'ios'
    } else if (Android) {
        return 'Android'
    }
}
// 截取url后面字段
export const getUrlParams = function (key: any) {
    let url = window.location.href;
    const arr = url.split("?");
    const newArr = arr[1] ? arr[1].split("&") : [];
    for (var i = 0; i < newArr.length; i++) {
        let temp = newArr[i].split("=");
        if (temp[0] === key) {
            let search = temp[1];
            if (search.indexOf("#") > 0) {
                search = search.substring(0, search.indexOf("#"));
            }
            return search;
        }
    }
}
export const copyText = (e:string | number) => {
    let dataNum = e + ''
    var element = createElement(dataNum);
    element.select();
    element.setSelectionRange(0, element.value.length);
    document.execCommand("copy");
    element.remove();
    xmtoast("已复制到剪切板");
};
const createElement = (text = "") => {
    var isRTL = document.documentElement.getAttribute("dir") === "rtl";
    var element = document.createElement("textarea");
    // 防止在ios中产生缩放效果
    element.style.fontSize = "12pt";
    // 重置盒模型
    element.style.border = "0";
    element.style.padding = "0";
    element.style.margin = "0";
    // 将元素移到屏幕外
    element.style.position = "absolute";
    element.style[isRTL ? "right" : "left"] = "-9999px";
    // 移动元素到页面底部
    let yPosition = window.pageYOffset || document.documentElement.scrollTop;
    element.style.top = `${yPosition}px`;
    //设置元素只读
    element.setAttribute("readonly", "");
    element.value = text;
    document.body.appendChild(element);
    return element;
};